package sg.edu.nus.iss.vmcs.customer;

import java.awt.*;
import java.awt.event.ActionListener;

import sg.edu.nus.iss.vmcs.maintenance.ButtonItem;
import sg.edu.nus.iss.vmcs.maintenance.ButtonItemDisplay;
import sg.edu.nus.iss.vmcs.store.*;
import sg.edu.nus.iss.vmcs.util.VMCSException;

//THN comment
public class CoinInputBox extends Panel {

	//static String[] Coins={"5c","10c","20c","50c","$1","Invalid"};
	private int len;
	public final static String TITLE = "Enter Coins Here";	
	private CoinReceiver cCtrl;
	
	private int[] coinWeights;
	
	private Label lb;
	private Button items[];
	
	public CoinInputBox(CoinReceiver controller){
		cCtrl = controller;
		
		Store cStore=cCtrl.getTransactionController().getMainController().getStoreController().getStore(Store.CASH);
		StoreItem[] coins=cStore.getItems();
		
		
		lb = new Label(TITLE);		
		
		Panel pp = new Panel();
		pp.setLayout(new GridLayout(1, 0));
		len=coins.length+1;
		items = new Button[len]; //Valid Coins + Invalid Coin
		coinWeights=new int[len];
		int i;
		Double weight=0.0;
		for (i = 0; i < len-1; i++) {
			items[i] =new Button(coins[i].getContent().getName());
			items[i].setBackground(Color.white);
			weight=((Coin)coins[i].getContent()).getWeight();			
			coinWeights[i]= weight.intValue();
			pp.add(items[i]);
		}	
		
		//Invalid Coin Button
		int lastIndex=items.length-1;
		items[lastIndex]=new Button("Invalid Coin");
		items[lastIndex].setBackground(Color.white);			
		coinWeights[lastIndex]=CashStore.INVALID_COIN_WEIGHT;
		pp.add(items[lastIndex]);
		
	
		Panel cpn = new Panel();
		cpn.setLayout(new BorderLayout());		
		cpn.add("West", lb);
		cpn.add("South", pp);
		
		this.setLayout(new GridLayout(0,1));
		this.add(cpn);	
		
		this.addListener(new CoinInputListener(this.cCtrl));
	}
	
	private void addListener(ActionListener l) {
		int i;
		for (i = 0; i < len; i++) {
			items[i].addActionListener(l);
			items[i].setActionCommand(String.valueOf(i));
		}
	}

	public void setActive(boolean st) {
		int i;
		lb.setEnabled(st);
		for (i = 0; i < len; i++) {
			items[i].setEnabled(st);
		}
	}
	
	//Extra Method
	/*
	 * @param return -1 if there index is out of bound.
	 */
	public int getCoinWeight(int coinIndex){
		if(coinIndex>=0 && coinIndex<this.coinWeights.length)
			return coinWeights[coinIndex];
		else
			return -1;
	}
}
